<?php defined("MAIN_DIR") or die("No direct access");

$id = $route->getPath(1);

$query = "select `Q`.*,`B`.`name`,`B`.`ozon`,`B`.`books`,COUNT(`R`.`mark`) as `rating`
    from `books` as `B`,`quotes` as `Q`
    left join `ratings` as `R`
        on `Q`.`id` = `R`.`quote_id`
    where
        `Q`.`id` = '{$id}' and `Q`.`status` = 'placed' and `Q`.`book_id` = `B`.`id`
    group by `Q`.`id`
    limit 1";

if (!$id){
    $query = "select `Q`.*,`B`.`name`,`B`.`ozon`,`B`.`books`,COUNT(`R`.`mark`) as `rating`
    from `books` as `B`,`quotes` as `Q`
    left join `ratings` as `R`
        on `Q`.`id` = `R`.`quote_id`
    where
        `Q`.`book_id` = `B`.`id` and `Q`.`status` = 'placed'
    group by `Q`.`id`
    order by RAND()
    limit 1";
}

$quote = $db->getRow($query);

// update looks counter
$query = "update `quotes` set `looks` = `looks` + 1 where `id` = '{$quote['id']}'";
$db->sendQuery($query);

$page_title = "Цитата #{$quote['id']} / Полезные цитаты";

$nQuote = $db->getCell("select `id` from `quotes` where `id` != '{$quote['id']}' order by RAND() limit 1");

require_once APP.HEADFILE;
?>

<input type="hidden" id="like_id" value="<?php echo $quote['id']; ?>" />

<div id="static-link">
    <a href="/<?php echo $quote['id']; ?>"># <?php echo $quote['id']; ?></a>
    <a id="next-quote" href="/<?php echo $nQuote; ?>">Следующая цитата &rarr;</a>
</div>

<h1 id="quote">
    <?php echo "<p>".str_replace("\n","</p><p>",str_replace("\r","",$quote['text']))."</p>"; ?>
</h1>

<div id="set">
    <div id="likes">
        <div id="<?php
            $ip = $_SERVER['REMOTE_ADDR'];
            if (isset($_COOKIE['sess_id'])){
                $session_id = $_COOKIE['sess_id'];
            } else {
                $session_id = md5(time().rand(0,100));
                setcookie("sess_id",$session_id,0,"/");
            }
            
            $is_vote = $db->getCell("select count(`id`) from `ratings` where `quote_id` = '{$quote['id']}' and (`ip` = '$ip' or `session_id` = '$session_id')");
            echo $is_vote == 0 ? "like" : "liked";
        ?>" title="Понравилось"></div>
        <div id="rating"><?php echo $quote['rating']; ?></div>
    </div>

    <div id="book">
        Отрывок из <a class="book-link" href='<?php echo $quote['ozon']; ?>'><?php echo "{$quote['name']}"; ?></a>
        <div id="book-links-wraper">
            <div id="bl_head">
                <a class="book-link" href='<?php echo $quote['ozon']; ?>'><?php echo "{$quote['name']}"; ?></a>
            </div>
            <div id="shop_links">
                <div id="ozon" class="shop">
                    <a class="book_link" href='<?php echo $quote['ozon']; ?>'>
                        <img src="http://static2.ozone.ru/multimedia/books_covers/c120/1001563239.jpg" /><br />
                        Заказать на OZON
                    </a>
                </div>
                <div id="books" class="shop">
                    <a class="book_link" href='<?php echo $quote['books']; ?>'>
                        <img src="http://static2.ozone.ru/multimedia/books_covers/c120/1001172817.jpg" /><br />
                        Заказать на Books.ru
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

<?php
	$query = "select `Q`.`id`,`Q`.`text`
		from `quotes` as `Q`
		left join `ratings` as `R`
			on `Q`.`id` = `R`.`quote_id`
		where
			`Q`.`id` != '{$quote['id']}' and
			`Q`.`book_id` = '{$quote['book_id']}'
		group by `Q`.`id`
		order by COUNT(`R`.`id`) desc, `Q`.`looks` desc
		limit 5";
	$similiars = $db->getTable($query);
	if ($similiars) {
		?><div id="similiar">
			<div id="similiar-header">Цитаты из той же книги</div>
			<ul id="similiar-list"><?php
				foreach ($similiars as $similiar) {
					echo "<li><a href='/{$similiar['id']}'>".cropText(strip_tags($similiar['text']),50)."</a></li>";
				}
			?></ul>
		</div><?php
	}
?>

<?php require_once APP.FOOTERFILE; ?>